package com.yzy.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;


/**
 * @author youzhiyong
 * @date 2020/9/23 11:51 上午
 */
public class MyNettyServer {

    public static void main(String[] args) {
        EventLoopGroup boosGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(boosGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            ChannelPipeline pipeline = socketChannel.pipeline();
                            pipeline.addLast("httpServerCodec", new HttpServerCodec());

                            pipeline.addLast("httpServerHandle", new SimpleChannelInboundHandler<HttpObject>() {
                                @Override
                                protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject) throws Exception {
                                    System.out.println("enter channelRead");
                                    if (httpObject instanceof HttpRequest) {
                                        HttpRequest request = (HttpRequest)httpObject;
                                        System.out.println("method:" +  request.method() + ", url:" + request.uri());
                                    }

                                    if (httpObject instanceof HttpContent) {
                                        HttpContent content = (HttpContent)httpObject;
                                        ByteBuf buffer = content.content();
                                        System.out.println(buffer.toString(CharsetUtil.UTF_8));

                                        ByteBuf byteBuf = Unpooled.copiedBuffer("hello word", CharsetUtil.UTF_8);
                                        FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, byteBuf);
                                        response.headers().add(HttpHeaderNames.CONTENT_TYPE, "text/plain");
                                        response.headers().add(HttpHeaderNames.CONTENT_LENGTH, byteBuf.readableBytes());

                                        channelHandlerContext.writeAndFlush(response);
                                    }
                                }
                            });
                        }
                    });
            ChannelFuture future = serverBootstrap.bind(8080).sync();
            future.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            boosGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

    }

}
